/////////////////////////////////////////////////////////////////////////////
/*/	
*	Creator:		Joshua C. Rouzer and Alexander Kahl
*	Title:			Single Player State cpp
*	Last Edited:	07/15/2013
*	Summary:		
/*/
/////////////////////////////////////////////////////////////////////////////
#include "SinglePlayerState.h"
#include "PauseMenuState.h"
#include "OptionsMenuState.h"
#include "MainMenuState.h"
#include "LevelManager.h"
#include "CSGD_TextureManager.h"
#include "CSGD_DirectInput.h"
#include "CSGD_XAudio2.h"
#include "BitmapFont.h"
#include "Game.h"
#include "Economy.h"
#include "CrateDrop.h"
#include <sstream>
#include "../resource.h"
#include "EntityManager.h"
#include "SGD_String.h"
using namespace std;

/////////////////////////////////////////////////////////////////////////////
// Function		: Default Constructor
// Notes		: Assigns default values to variables and loads necesarry files
/////////////////////////////////////////////////////////////////////////////
CSinglePlayerState::CSinglePlayerState(void)
{
	m_fTimeCount = 0.0f;
	m_bPaused = false;
	m_nPlayerDeath1 = CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("resource/Sounds/UnitDeath.wav"));
	m_nLaneImg = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/lanes.png"));

	m_pEventM = CEventManager::GetInstance();

	m_fEconomyTimer = 0.05f;

	bPause = false;
}

/////////////////////////////////////////////////////////////////////////////
// Function		: ~CSinglePlayerState 
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
CSinglePlayerState::~CSinglePlayerState(void)
{
}
/////////////////////////////////////////////////////////////////////////////
// Function		: GetInstance 
// Return		: CSinglePlayerState*
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
CSinglePlayerState* CSinglePlayerState::GetInstance( void )
{
	static CSinglePlayerState s_Instance;

	return &s_Instance;
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Enter 
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CSinglePlayerState::Enter( void )
{
	if( !bPause )
	{
		//CLevelManager::GetInstance()->SetCurrLevel( 0 );
		CLevelManager::GetInstance()->GetCurrLevel()->Enter();
		if( CLevelManager::GetInstance()->GetCurrLevel()->m_bBossLevel == false )
			CLevelManager::GetInstance()->GetCurrLevel()->GetTeam2()->GetPlayerShip()->SetAI(true);
		bPause = false;
	}

	if(COptionsMenuState::GetInstance()->ReturnEng())
	{
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_Units, Units, 20);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_Money, Money, 20);
	}
	else
	{
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_Units, Units, 20);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_Money, Money, 20);
	}

	sMoney = "";

	for (int i = 0; i < 20; i++)
	{
		if(Money[i] != NULL)
			sMoney += (char)Money[i];

		else
			break;
	}
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Exit 
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CSinglePlayerState::Exit ( void )
{
	if( !bPause || CLevelManager::GetInstance()->GetCurrLevel()->GetTeam1()->GetBase()->GetHealth() <= 0 )
	{
		CLevelManager::GetInstance()->GetCurrLevel()->Exit();
		CEntityManager::GetInstance()->Clear();
	}
	else if( CLevelManager::GetInstance()->GetCurrLevel()->m_bBossLevel == false )
	{
		if( CLevelManager::GetInstance()->GetCurrLevel()->GetTeam2()->GetBase()->GetHealth() <= 0 )
		{
			CLevelManager::GetInstance()->GetCurrLevel()->Exit();
			CEntityManager::GetInstance()->Clear();
		}
	}
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Input 
// Return		: bool
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
bool CSinglePlayerState::Input( void )
{
	CSGD_DirectInput* m_pDI = CSGD_DirectInput::GetInstance();

	if(m_pDI->KeyDown(DIK_LALT) && m_pDI->KeyPressed(DIK_TAB))
	{
		CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetConfirmID());
		bPause = true;
		CPauseMenuState::GetInstance()->SetPrevState(*this);
		CGame::GetInstance()->ChangeState(CPauseMenuState::GetInstance());
	}
	else if( m_pDI->KeyPressed( DIK_ESCAPE ) == true || m_pDI->JoystickButtonPressed(7,0) )
	{
		CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetConfirmID());
		bPause = true;
		CPauseMenuState::GetInstance()->SetPrevState(*this);
		CGame::GetInstance()->ChangeState(CPauseMenuState::GetInstance());
	}

	return true;	
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Update 
// Parameters	: fElapsedTime - Ammount of time passed since last update
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CSinglePlayerState::Update( float fElapsedTime )
{
	m_fEconomyTimer -= fElapsedTime;

	if(m_fEconomyTimer <= 0.0f)
	{
		m_fEconomyTimer = 5.0f;

		CCrateDrop::GetInstance()->CreateCrate();

		CEconomy::GetInstance()->AddCredits(15);
	}

	// Update current level
	CLevelManager::GetInstance()->GetCurrLevel()->Update( fElapsedTime );


	m_pEventM->ProcessEvents();
	m_fTimeCount += fElapsedTime;

}
/////////////////////////////////////////////////////////////////////////////
// Function		: Render 
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CSinglePlayerState::Render( void )
{
	CLevelManager::GetInstance()->GetCurrLevel()->Render();
	if(CLevelManager::GetInstance()->GetLevelID() == 0){
		CSGD_TextureManager::GetInstance()->Draw(m_nLaneImg,0,0);
	}

	CBitmapFont* font = CGame::GetInstance()->GetFont();

	wostringstream TMP;
	TMP << Units;
	unsigned int count = 0;
	for( unsigned int i = 0; i < CEntityManager::GetInstance()->m_BaseUnits.size(); ++i )
	{
		if( dynamic_cast<CBaseUnit*>(CEntityManager::GetInstance()->m_BaseUnits[i]) )
			count++;
	}
	TMP << count;
	font->Draw(TMP.str().c_str(),280,50,1,D3DCOLOR_ARGB( 255, 255, 255, 51 ) );

	TMP.str(_T(""));

	//wostringstream P1;
	TMP << Money  << ": " << CLevelManager::GetInstance()->GetCurrLevel()->GetTeam1()->GetPlayerShip()->GetCredits();

	font->Draw(TMP.str().c_str(), 0,53,0.8f,D3DCOLOR_ARGB(255,255,255,255));

	TMP.str(_T(""));

	if( CLevelManager::GetInstance()->GetCurrLevel()->m_bBossLevel == false )
	{
		TMP << Money << ": " << CLevelManager::GetInstance()->GetCurrLevel()->GetTeam2()->GetPlayerShip()->GetCredits();

		int strwidth = CGame::GetInstance()->GetFont()->GetStringWidth(sMoney,0.8f);
		stringstream numcredits;
		numcredits << ": " << CLevelManager::GetInstance()->GetCurrLevel()->GetTeam2()->GetPlayerShip()->GetCredits();
		strwidth += CGame::GetInstance()->GetFont()->GetStringWidth(numcredits.str(), 0.8f);
		font->Draw(TMP.str().c_str(), 800 - strwidth,53,0.8f,D3DCOLOR_ARGB(255,255,255,255));
	}
}